﻿using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace OMS.Infrastructure
{
    public class LogActionFilter : ActionFilterAttribute
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        public LogActionFilter()
        {
            log = LogManager.GetCurrentClassLogger();
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var username = filterContext.RequestContext.HttpContext.User.Identity.Name;
            Log("OnActionExecuting", username, filterContext.RouteData);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //var username = filterContext.RequestContext.HttpContext.User.Identity.Name;
            //Log("OnActionExecuted", username, filterContext.RouteData);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            //var username = filterContext.RequestContext.HttpContext.User.Identity.Name;
            //Log("OnResultExecuting", username, filterContext.RouteData);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            //var username = filterContext.RequestContext.HttpContext.User.Identity.Name;
            //Log("OnResultExecuted", username, filterContext.RouteData);
        }

        private void Log(string methodName, string username, RouteData routeData)
        {
            var controllerName = routeData.Values["controller"];
            var actionName = routeData.Values["action"];
            var message = String.Format("{0} user:{1} controller:{2} action:{3}", methodName, username, controllerName, actionName);
            log.Info(message);
        }

    }
}